<?php


namespace app\app\controller;


use app\app\model\ProjectModel;
use app\app\model\ProjectProcessSplit;
use app\app\model\ProjectTemplate;
use app\app\model\ProjectTemplateRecord;
use app\app\model\SysFileInfoModel;
use app\BaseController;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;

class ProjectEngineeringSample extends BaseController
{

    /**
     * 获取项目列表
     */
    public function getProjectList()
    {
        $list = ProjectTemplateRecord::alias('a')
            ->join('t_project_template b', 'b.id = a.template_id')
            ->join('t_project c', 'c.id = b.project_id')
            ->field('count(c.project_name) as all_count,c.id,c.link_project_id')
            ->group('c.link_project_id')
            ->select();
        $project_son = ProjectTemplateRecord::alias('a')
            ->join('t_project_template b', 'b.id = a.template_id')
            ->join('t_project c', 'c.id = b.project_id')
            ->field('count(c.project_name) as all_count,c.id,c.link_project_id')
            ->group('c.id')
            ->select();
        $not_ture = ProjectTemplateRecord::alias('a')
            ->join('t_project_template b', 'b.id = a.template_id')
            ->where('process_photos', null)
            ->column('b.project_id');


        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['is_parent', '=', 0];
        $where[] = ['tc_time', '>=', $start_time];
        $where[] = ['kg_time', '<=', $end_time];
        $link_project_ids = ProjectModel::where($where)
            ->column('project_name', 'id');
        foreach ($list as $k => $v) {
            $list[$k]['project_name'] = $link_project_ids[$v['link_project_id']];
        }
        foreach ($not_ture as $key => $value) {
            foreach ($project_son as $k => $v) {
                if ($value == $v['id']) {
                    $project_son[$k]['not_count'] += 1;
                }
            }
        }
        foreach ($project_son as $k => $v) {
            foreach ($list as $key => $value) {
                if ($value['link_project_id'] == $v['link_project_id']) {
                    $list[$key]['not_count'] += $v['not_count'];
                }
            }
        }
        $this->success('获取成功', $list);

    }

    /**
     * 获取项目子集
     */
    public function getProjectListSon()
    {
        $data = $this->request->param();
        if (empty($data['project_id'])) {
            $this->error('id异常');
        }
        $list = ProjectTemplateRecord::alias('a')
            ->join('t_project_template b', 'b.id = a.template_id')
            ->join('t_project c', 'c.id = b.project_id')
            ->where('c.link_project_id', $data['project_id'])
            ->where('c.is_parent', 1)
            ->field('count(c.project_name) as all_count,c.id,c.link_project_id,c.project_name')
            ->group('c.id')
            ->select();
        $not_ture = ProjectTemplateRecord::alias('a')
            ->join('t_project_template b', 'b.id = a.template_id')
            ->where('process_photos', null)
            ->column('b.project_id');
        foreach ($list as $k => $v) {
            $list[$k]['not_count'] = 0;
        }
        foreach ($not_ture as $key => $value) {
            foreach ($list as $k => $v) {
                if ($value == $v['id']) {
                    $list[$k]['not_count'] += 1;
                }
            }
        }
        $this->success('获取成功', $list);
    }

    /**
     * 获取样板
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProjectTemplate()
    {
        $data = $this->request->param();
        if (empty($data['project_id'])) {
            $this->error('id异常');
        }
        $list = ProjectTemplate::alias('a')
            ->leftJoin('t_project_template_record b', 'b.template_id = a.id')
            ->where('a.project_id', $data['project_id'])
            ->group('a.id')
            ->field('count(b.id) as all_count,a.id,a.area,a.template_name')
            ->select();
        $not_ture = ProjectTemplateRecord::alias('a')
            ->join('t_project_template b', 'b.id = a.template_id')
            ->where('process_photos', null)
            ->where('b.project_id', $data['project_id'])
            ->column('b.id');
        foreach ($list as $k => $v) {
            $list[$k]['not_count'] = 0;
        }
        foreach ($not_ture as $key => $value) {
            foreach ($list as $k => $v) {
                if ($value == $v['id']) {
                    $list[$k]['not_count'] += 1;
                }
            }
        }
        $this->success('获取成功', $list);
    }

    /**
     * 获取样板详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProjectTemplateInfo()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('id异常');
        }
        $data = ProjectTemplate::find($id);
        if (empty($data)) {
            $this->error('获取样板异常');
        }
        $this->success('获取成功', $data);
    }

    /**
     * 获取样板记录
     */
    public function getProjectTemplateRecord()
    {
        $templateId = $this->request->param('template_id');
        $data = ProjectTemplateRecord::alias('a')
            ->leftjoin('t_project_template b', 'b.id = a.template_id')
            ->where('b.id', $templateId)
            ->field('a.process_split,a.id,a.process_photos,b.area,b.template_name')
            ->select();
        $file_data = SysFileInfoModel::column('file_path', 'file_id');
        foreach ($data as $key => $value) {
            if (empty($value['process_photos'])) {
                $data[$key]['process_photos_path'] = '';
            } else {
                $data[$key]['process_photos_path'] = str_replace('\\', '/', $file_data[$value['process_photos']]);
            }
        }
        $this->success('ok', $data);
    }

    /**
     * 获取工序拆分
     */
    public function getProjectProcessSplit()
    {
        $id = $this->request->param('template_id');
        if (empty($id)) {
            $this->error('template_id不可为空');
        }
        $list = ProjectTemplateRecord::field('process_split,id')
            ->where('template_id', $id)
            ->where('process_photos', null)
            ->select();
        $this->error('获取成功', $list);
    }

    /**
     * 查看记录详情
     */
    public function getRecordInfo()
    {
        $id = $this->request->param('record_id');
        $data = ProjectTemplateRecord::alias('a')
            ->leftjoin('t_project_template b', 'b.id = a.template_id')
            ->where('a.id', $id)
            ->field('a.process_split,a.id,a.process_photos,b.area,b.template_name,b.drawing_require,b.acceptance_specification')
            ->find();
        $data['process_photos_path'] = SysFileInfoModel::where('file_id', $data['process_photos'])
            ->value('file_path');
        if (!empty($data['process_photos_path'])) {
            $data['process_photos_path'] = str_replace('\\', '/', $data['process_photos_path']);
        }
        $this->success('ok', $data);
    }

    /**
     * 样板记录添加
     */
    public function addTemplate()
    {
        $id = $this->request->param('id');
        $process_photos = $this->request->param('process_photos');
        if (empty($id)) {
            $this->error('拆分id不可为空');
        }
        if (empty($process_photos)) {
            $this->error('图片id不可为空');
        }
        $res = ProjectTemplateRecord::where('id', $id)
            ->update([
                'process_photos' => $process_photos
            ]);
        if (!$res) {
            $this->error('图片添加失败');
        }
        $this->success('图片添加成功');
    }

//    /**
//     * 添加样板
//     */
//    public function addTemplate()
//    {
//        $data = $this->request->param();
//        try {
//            validate(\app\app\Validate\ProjectEngineeringSample::class)->check([
//                'project_id' => $data['project_id'],
//                'area' => $data['area'],
//                'template_name' => $data['template_name'],
//                'drawing_require' => $data['drawing_require'],
//                'acceptance_specification' => $data['acceptance_specification'],
//            ]);
//        } catch (ValidateException $e) {
//            // 验证失败 输出错误信息
//            $this->error($e->getError());;
//        }
//
//        Db::startTrans();
//        try {
//            $res = ProjectTemplate::create([
//                'project_id' => $data['project_id'],
//                'area' => $data['area'],
//                'template_name' => $data['template_name'],
//                'drawing_require' => $data['drawing_require'],
//                'acceptance_specification' => $data['acceptance_specification'],
//            ]);
//            $recordArray = explode(',', $data['record']);
//            if (!$res) {
//                $this->error('添加异常');
//            }
//            //分别添加样板记录
//            foreach ($recordArray as $key => $value) {
//                $re[] = ProjectTemplateRecord::insert([
//                    'process_split' => $value,
//                    'acceptance_release' => $data['acceptance_release'],
//                    'template_id' => $res['id']
//                ]);
//            }
//            if (in_array('0', $re) || !$res) {
//                throw new Exception('添加异常');
//            }
//            Db::commit();
//            $this->success('添加完成！');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//
//        $this->success('ok', $res);
//    }


}